CLANG ?= clang
LLVM_STRIP ?= llvm-strip
BPFTOOL ?= bpftool

ARCH := $(shell uname -m | sed 's/x86_64/x86/' | sed 's/aarch64/arm64/')
INCLUDES := -I../headers

.PHONY: all clean

all: open

open: vmlinux.h open.bpf.o open.skel.h open.o
	$(CC) $(CFLAGS) -o $@ open.o -lelf -lz -lbpf

open.o: open.c open.skel.h
	$(CC) $(CFLAGS) -c $<

open.skel.h: open.bpf.o
	$(BPFTOOL) gen skeleton $< > $@

open.bpf.o: open.bpf.c vmlinux.h
	$(CLANG) -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) $(INCLUDES) -c $< -o $@
	$(LLVM_STRIP) -g $@ # 去除调试信息

vmlinux.h:
	bpftool btf dump file /sys/kernel/btf/vmlinux format c > vmlinux.h

clean:
	rm -f *.o
	rm -f open
	rm -f open.skel.h
	rm -f vmlinux.h